package com.nk;

import javax.swing.tree.TreeNode;

/**
 * @ClassName Main3
 * @Description 描述
 * @Detail detail
 * @Author MyPC
 * @Date 2020/12/31
 * @Version 1.0
 */
public class Main3 {
    public static void main(String[] args) {
        Solution.TreeNode root = new Solution.TreeNode(1);
        //[1,2,2,3,null,null,3,4,null,null,4]
        root.left=new Solution.TreeNode(2);
        root.right=new Solution.TreeNode(2);
        root.left.left=new Solution.TreeNode(3);
        root.right.right=new Solution.TreeNode(4);
    }
}

class Solution {
    public static boolean isBalanced(TreeNode root) {
        int d1=0;
        int d2=0;
        if(root==null){
            return true;
        }
        if(root.left!=null){
            d1=deep(root.left);
        }
        if(root.right!=null){
            d2=deep(root.right);
        }
        return Math.abs(d1 - d2) <= 1;
    }
    public static int deep(TreeNode root){
        return root==null ? 0 : Math.max(deep(root.left),deep(root.right))+1;
    }

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }
}

